package com.xiaojie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaojie.entity.User;
import com.xiaojie.service.IUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.management.ObjectName;
import java.rmi.MarshalledObject;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author xiaojie
 * @since 2022-07-28
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @GetMapping("/users")
    public Map<String,Object> selectUserAll(@RequestParam("page") int curPage,
                                            @RequestParam("per_page") int pageSize,
                                            @RequestParam(value = "id",required = false) Integer uid,
                                            String name, String phone){
        IPage page = new Page<>(curPage, pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        // 有判断name是不是空字符串
        if (!StringUtils.isEmpty(name)){
            wrapper.eq("name",name);
        }
        //及判断id是不是空
        if (!ObjectUtils.isEmpty(uid)){
            wrapper.eq("id",uid);
        }
        if (!StringUtils.isEmpty(phone)){
            wrapper.eq("phone",phone);
        }
        IPage page1 = userService.page(page, wrapper);
        Map<String,Object> map=new HashMap<>();
        //总条数
        map.put("total_count",page1.getTotal());
        //当前页信息
        map.put("items",page1.getRecords());
        return map;
    }

}

